package com.books.service.alipay.impl;

import com.books.model.entity.common.People;
import com.books.model.entity.order.BuyOrder;
import com.books.model.entity.order.OrderList;
import com.books.model.vo.PayAsyncVo;
import com.books.model.vo.PayVo;
import com.books.model.vo.TransactionStatus;
import com.books.service.admin.IAdminService;
import com.books.service.alipay.OrderService;
import com.books.service.order.IBuyOrderServer;
import com.books.service.order.IOrderListServer;
import com.books.service.people.IPeopleService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ZhouRui
 * @title: 码上就来
 * @projectName cbstore-spring-boot
 * @description: TODO
 * @date 2021/10/1320:06
 */

@Service
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private PayVo payVo;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private IBuyOrderServer buyOrderServer;

    @Autowired
    private BuyOrder buyOrder;

    @Autowired
    private IPeopleService peopleService;

    @Autowired
    private IAdminService adminService;



    @Override
    public PayVo getOrderPay(String orderSn,String id) {
        // 生成一个订单
        // 订单号
        buyOrder= (BuyOrder) redisTemplate.opsForValue().get("ocode"+id);
        payVo.setOutTradeNo(orderSn);
        payVo.setSubject("云上书屋出品");
        payVo.setBuyDate(buyOrder.getBuyTime());
        payVo.setTotalAmount(buyOrder.getTotalMoney());
        payVo.setBuyId(buyOrder.getBuyId());
        return payVo;
    }

    /**
     * 处理支付宝支付结果
     * @param vo 支付信息
     * @return
     */
    @Override
    public String handlePayResult(PayAsyncVo vo) {
        // 可以将vo里的相关支付信息保存到数据库....
        // 判断是否支付成功 - 获取状态，只有以下两种状态是支付成功了
        if (TransactionStatus.TRADE_SUCCESS.equals(vo.getTrade_status()) || TransactionStatus.TRADE_FINISHED.equals(vo.getTrade_status())) {
            // 支付成功状态 - 可以根据订单号修改数据库里订单状态为已支付....
            String outTradeNo = vo.getOut_trade_no();
            System.out.println("支付成功");
            payVo= (PayVo) redisTemplate.opsForValue().get("pcode"+outTradeNo);
            redisTemplate.delete("pcode"+outTradeNo);
            buyOrder= (BuyOrder) redisTemplate.opsForValue().get("ocode"+payVo.getBuyId());
            People people=peopleService.selectPerson(buyOrder.getUserId());
            people.setVipIntegral(people.getVipIntegral()+buyOrder.getTotalMoney());
            if(people.getVipIntegral()>=adminService.findByVipId(1).getVipPrice()){
                people.setVipId(1);
            }
            if(people.getVipIntegral()>=adminService.findByVipId(2).getVipPrice()){
                people.setVipId(2);
            }
            if(people.getVipIntegral()>=adminService.findByVipId(3).getVipPrice()){
                people.setVipId(3);
            }
            if(people.getVipIntegral()>=adminService.findByVipId(4).getVipPrice()){
                people.setVipId(4);
            }
            if(people.getVipIntegral()>=adminService.findByVipId(5).getVipPrice()){
                people.setVipId(5);
            }
            if(people.getVipIntegral()>=adminService.findByVipId(6).getVipPrice()){
                people.setVipId(6);
            }
            adminService.updateVipIntegral(people.getVipIntegral(),people.getVipId(),people.getPeopleId());
            redisTemplate.delete("ocode"+payVo.getBuyId());
            buyOrderServer.addOrder(buyOrder);
            List<OrderList> orderLists;
            orderLists= (List<OrderList>) redisTemplate.opsForValue().get("bcode"+payVo.getBuyId());
            redisTemplate.delete("bcode"+payVo.getBuyId());
            rabbitTemplate.convertAndSend("cloudbook_fanout","",orderLists);
            return "success";
        }
        System.out.println("支付失败");
        return "fail";
    }
}
